home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Ken Long / NewBounce-c / bounce.c next >
Encoding:
Text File  |  1994-12-04  |  4.3 KB  |  153 lines  |  [TEXT/MMCC]

  1. //•———————————————————————————————•••———————————————————————————————•//
  2. //•—————————————————————— Another old C source —————————————————————•//
  3. //•—————————————————— "brought back from the dead" —————————————————•//
  4. //•——————————————————————— by Kenneth A. Long ——————————————————————•//
  5. //•——————————————————————— at itty bitty bytes™ ————————————————————•//
  6. //•————————————— Made to run on Think C™ on 13 March 1994 ——————————•//
  7. //•————————— Made to run on Code Warrior on 20 November 1994 ———————•//
  8. //•———————————————————————————————•••———————————————————————————————•//
  9.  
  10. //• Written  1:17 pm  Jun 12, 1986
  11. //• by Steve Hawley
  12. //• sdh@joevax.UUCP
  13. //• Posted on uiucdcsb:net.sources.mac
  14. //• "Sleep Utility"
  15.  
  16. //• The following is the source to a sleep utility. It is equivalent 
  17. //• to the blit demo bounce. It clears the *entire* screen to black 
  18. //• thus allowing you to leave your mac on w/o having the menu bar 
  19. //• burned in.
  20.  
  21.  
  22. #define range 12;
  23. #define size 32        //• Should be a power of 2.
  24. #define bound_X qd.screenBits.bounds.right
  25. #define bound_Y qd.screenBits.bounds.bottom
  26.  
  27. GrafPort myPort;
  28.  
  29. int x1[size], x2[size], y1[size], y2[size], dx1, dx2, dy1, dy2;
  30.  
  31.  
  32. //o Bounce - Steve Hawley 6/13/86 Written in Aztec C V 1.06H.
  33.  
  34. //o This is a Mac implementation of the bounce program written for
  35. //o the BLIT and 5620 terminals. It simply keeps track of a list of
  36. //o end points of lines, erasing the oldest and replacing it with 
  37. //o one derived from the newest plus some dx and dy values.
  38.  
  39.  
  40. int abs (int i)
  41. {
  42.  
  43.     //o Absolute value.
  44.     if (i < 0) 
  45.         return - i;
  46.     else 
  47.         return i;
  48. }
  49.  
  50. WindowPtr    genericWindow;
  51. Rect        dragRect;
  52. Rect        windowBounds; 
  53.  
  54. void SetUpWindow (void)            //• A KAL addition.
  55. {
  56.     dragRect = qd.screenBits.bounds;
  57.     
  58.     SetRect (&windowBounds, 0, 0, bound_X, bound_Y);
  59.     genericWindow = NewWindow (0L, &windowBounds, "\pBullseye", true, noGrowDocProc, (WindowPtr) -1L, true, 0);
  60.     FillRect (&windowBounds, &qd.black);
  61.     SetPort (genericWindow);
  62. }
  63.  
  64. init ()
  65. {
  66.     int i;
  67.     
  68.     dx1 = Random () % range + 4; /* create random dx's and dy's */
  69.     dx2 = Random () % range + 4;
  70.     dy1 = Random () % range + 4;
  71.     dy2 = Random () % range + 4;
  72.     
  73.     x1[0] = abs (Random () % bound_X) + 1; /* create first point */
  74.     x2[0] = abs (Random () % bound_X) + 1;
  75.     y1[0] = abs (Random () % bound_Y) + 1;
  76.     y2[0] = abs (Random () % bound_Y) + 1;
  77.     
  78.     MoveTo (x1[0], y1[0]);
  79.     LineTo (x2[0], y2[0]); /* draw it */
  80.     
  81.     for (i = 1; i < size; i++) 
  82.     {   /* derive the rest of the points */
  83.        if ((x1[i - 1] + dx1 < 0) || (x1[i - 1] + dx1 > bound_X))
  84.           dx1 = - dx1;
  85.        x1[i] = x1[i - 1] + dx1;
  86.        if ((x2[i - 1] + dx2 < 0) || (x2[i - 1] + dx2 > bound_X))
  87.           dx2 = - dx2;
  88.        x2[i] = x2[i - 1] + dx2;
  89.        if ((y1[i - 1] + dy1 < 0) || (y1[i - 1] + dy1 > bound_Y))
  90.           dy1 = - dy1;
  91.        y1[i] = y1[i - 1] + dy1;
  92.        if ((y2[i - 1] + dy2 < 0) || (y2[i - 1] + dy2 > bound_Y))
  93.           dy2 = - dy2;
  94.        y2[i] = y2[i - 1] + dy2;
  95.        MoveTo (x1[i], y1[i]); /* draw them */
  96.        LineTo (x2[i], y2[i]);
  97.     }
  98. }
  99.  
  100.  
  101. main ()
  102. {
  103.     Rect myRect;
  104.     register int i, j;
  105.     GrafPtr savePort;
  106.     
  107.     InitGraf (&qd.thePort);
  108.  
  109.     InitWindows ();        //• Added by Ken Long because blackening the
  110.                         //• whole screen (next comment) did not
  111.                         //• UNblacken!
  112.     SetUpWindow ();
  113.     
  114.     OpenPort (&myPort);     //o I create my own port so I don't need
  115.                            //o windows and can blacken the whole screen.
  116.     
  117.     
  118.     HideCursor ();
  119.     
  120.     SetRect (&myRect, 0, 0, bound_X, bound_Y);
  121.     PenPat (&qd.black);
  122.     PaintRect (&myRect);    //o Clear screen to black.
  123.     PenMode (patXor);    //o use exclusive-or drawing to ease animation.
  124.     init ();
  125.     i = 0;        //o Oldest set of points.
  126.     
  127.     while (!Button ())        //o repeat until button is down.
  128.     {
  129.        j = i - 1;         //o find newest points.
  130.        if (j< 0) j = size - 1;
  131.        MoveTo (x1[i], y1[i]);        //o erase old.
  132.        LineTo (x2[i], y2[i]);
  133.        //o derive new point */
  134.        if ((x1[j] + dx1 < 0) || (x1[j] + dx1 > bound_X))
  135.           dx1 = - dx1;
  136.        x1[i] = x1[j] + dx1;
  137.        if ((x2[j] + dx2 < 0) || (x2[j] + dx2 > bound_X))
  138.           dx2 = - dx2;
  139.        x2[i] = x2[j] + dx2;
  140.        if ((y1[j] + dy1 < 0) || (y1[j] + dy1 > bound_Y))
  141.           dy1 = - dy1;
  142.        y1[i] = y1[j] + dy1;
  143.        if ((y2[j] + dy2 < 0) || (y2[j] + dy2 > bound_Y))
  144.           dy2 = - dy2;
  145.        y2[i] = y2[j] + dy2;
  146.        MoveTo (x1[i], y1[i]);        //o draw new line.
  147.        LineTo (x2[i], y2[i]);
  148.        i = (i + 1) & size - 1;        //o get oldest line.
  149.     }
  150. //    SetPort (savePort);
  151. }
  152. //o End of text from uiucdcsb:net.sources.mac.
  153.